package com.fulei.logic;

import android.annotation.SuppressLint;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

@SuppressLint({"SdCardPath"})
public class CoinOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  public static int eventCount = 0;
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int ResetCoin()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 3, 0, 1, 1, -5 };
      
      LogUtil.writeCoinAcceptorInfo("ResetCoinSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "ResetCoinRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 3)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeCoinAcceptorInfo("硬币器复位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int coinClearEvent()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 0, 1, 1, -4 };
      
      LogUtil.writeCoinAcceptorInfo("ResetCoinSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "ResetCoinRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 3)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeCoinAcceptorInfo("硬币器清空事件数异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int SelfCheck()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 0, 1, -24, 21 };
      
      LogUtil.writeCoinAcceptorInfo("SelfCheckSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "SelfCheckRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 10)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("SelfCheck:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int TestSoleNoid()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 1, 1, -16, 1, 11 };
      
      LogUtil.writeCoinAcceptorInfo("TestSoleNoidSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "TestSoleNoidRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 10)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("TestSoleNoid:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int UseCoinPass()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 2, 1, -25, 3, 0, 17 };
      
      LogUtil.writeCoinAcceptorInfo("UseCoinPassSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "UseCoinPassRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 10)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("UseCoinPass:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int DisableCoinPass()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 2, 1, -25, 0, 0, 20 };
      
      LogUtil.writeCoinAcceptorInfo("DisableCoinPassSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "DisableCoinPassRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 10)) {
        return_data = 1;
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("DisableCoinPass:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int coinPoll()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 0, 1, -27, 24 };
      
      LogUtil.writeCoinAcceptorInfo("coinPollSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      if ((ReadData != null) && (ReadData[5] == 1) && (ReadData[6] == 11) && (ReadData[7] == 2) && (ReadData[8] == 0))
      {
        LogUtil.writeCoinAcceptorInfo(
          "coinPollRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
        



        byte Sum = 0;
        for (int i = 0; i < ReadData.length - 1; i++) {
          Sum = (byte)(Sum + ReadData[i]);
        }
        byte CheckSum = (byte)(255 - Sum + 1);
        if (CheckSum == ReadData[(ReadData.length - 1)])
        {
          LogUtil.writeCoinAcceptorInfo("eventCount:" + eventCount);
          
          int return_eventCount = ReadData[9];
          if (ReadData[9] != eventCount)
          {
            if (return_eventCount == 0)
            {
              eventCount = 0;
              return -2;
            }
            if (ReadData[9] <= eventCount)
            {
              int EvCount = ReadData[9] + 255 - eventCount;
              eventCount = ReadData[9];
              
              int GotMoney = GetEventCount(ReadData, EvCount);
              if (GotMoney > 0) {
                return_data = GotMoney;
              }
            }
            else if (ReadData[9] > eventCount)
            {
              int EvCount = ReadData[9] - eventCount;
              eventCount = ReadData[9];
              int GotMoney = GetEventCount(ReadData, EvCount);
              if (GotMoney > 0) {
                return_data = GotMoney;
              }
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("coinPoll：" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CheckEventCount()
  {
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 2, 0, 1, -27, 24 };
      
      LogUtil.writeCoinAcceptorInfo("CheckEventCountSend:" +
        LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeCoinAcceptorInfo(
        "CheckEventCountRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData != null) && (ReadData.length > 10)) {
        eventCount = ReadData[9];
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("CheckEventCount:" + ex.getMessage());
    }
    return eventCount;
  }
  
  private static int GetEventCount(byte[] ReadData, int count)
  {
    int GotMoney = 0;
    try
    {
      if (count == 1)
      {
        if ((ReadData[10] == 1) && (ReadData[11] == 2)) {
          GotMoney = ReadData[10] * 2;
        }
        if ((ReadData[10] == 2) && (ReadData[11] == 3)) {
          GotMoney = ReadData[10] / 2;
        }
      }
      else if (count == 2)
      {
        if (((ReadData[10] == 1) && (ReadData[11] == 2)) || ((ReadData[12] == 1) && (ReadData[13] == 2))) {
          GotMoney = (ReadData[10] + ReadData[12]) * 2;
        }
        if (((ReadData[10] == 2) && (ReadData[11] == 3)) || ((ReadData[12] == 2) && (ReadData[13] == 3))) {
          GotMoney = (ReadData[10] + ReadData[12]) / 2;
        }
      }
      else if (count == 3)
      {
        if (((ReadData[10] == 1) && (ReadData[11] == 2)) || ((ReadData[12] == 1) && (ReadData[13] == 2)) || ((ReadData[14] == 1) && (ReadData[15] == 2))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14]) * 2;
        }
        if (((ReadData[10] == 2) && (ReadData[11] == 3)) || ((ReadData[12] == 2) && (ReadData[13] == 3)) || ((ReadData[14] == 2) && (ReadData[15] == 3))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14]) / 2;
        }
      }
      else if (count == 4)
      {
        if (((ReadData[10] == 1) && (ReadData[11] == 2)) || ((ReadData[12] == 1) && (ReadData[13] == 2)) || ((ReadData[14] == 1) && (ReadData[15] == 2)) || ((ReadData[16] == 1) && (ReadData[17] == 2))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14] + ReadData[16]) * 2;
        }
        if (((ReadData[10] == 2) && (ReadData[11] == 3)) || ((ReadData[12] == 2) && (ReadData[13] == 3)) || ((ReadData[14] == 2) && (ReadData[15] == 3)) || ((ReadData[16] == 2) && (ReadData[17] == 3))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14] + ReadData[16]) / 2;
        }
      }
      else
      {
        if (((ReadData[10] == 1) && (ReadData[11] == 2)) || ((ReadData[12] == 1) && (ReadData[13] == 2)) || ((ReadData[14] == 1) && (ReadData[15] == 2)) || ((ReadData[16] == 1) && (ReadData[17] == 2)) || ((ReadData[18] == 1) && (ReadData[19] == 2))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14] + ReadData[16] + ReadData[18]) * 2;
        }
        if (((ReadData[10] == 2) && (ReadData[11] == 3)) || ((ReadData[12] == 2) && (ReadData[13] == 3)) || ((ReadData[14] == 2) && (ReadData[15] == 3)) || ((ReadData[16] == 2) && (ReadData[17] == 3)) || ((ReadData[18] == 2) && (ReadData[19] == 3))) {
          GotMoney = (ReadData[10] + ReadData[12] + ReadData[14] + ReadData[16] + ReadData[18]) / 2;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeCoinAcceptorInfo("GetEventCount:" + ex.getMessage());
    }
    return GotMoney;
  }
}
